package cn.lanqiao.web.controller;

import cn.lanqiao.common.entity.Film;
import cn.lanqiao.common.utile.Result;
import cn.lanqiao.common.vo.FilmCheckVo;
import cn.lanqiao.common.vo.FilmVo;
import cn.lanqiao.system.service.FilmService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/film")
@CrossOrigin
public class FilmController {

    @Autowired
    private FilmService filmService;
    @RequestMapping("/getAll")
    public Result getAll(){
        List<FilmVo> list=filmService.getAll();
        return Result.ok(list);
    }

    //模糊查询
    @RequestMapping("/findByName")
    public Result findByName(String value){
        System.out.println("-----"+value);
        try {
            List<FilmVo> list=filmService.findByName(value);
            return Result.ok(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail("系统错误！");
    }

    @RequestMapping("/insertFilm")
    public Result insertFilm(Film film) {
        int r = filmService.insert(film);
        if (r > 0) {
            return Result.ok();
        }else{
            return Result.fail("添加失败") ;
        }
    }
    @RequestMapping("/updateFilm")
    public Result updateFilm(Film film){
       int r=filmService.updateByPrimaryKey(film);
        if (r > 0) {
            return Result.ok();
        }else{
            return Result.fail("修改失败") ;
        }
    }
    @RequestMapping("deleteFilm")
    public Result deleteFilm(Integer id) {
        int r = filmService.deleteByPrimaryKey(id);
            if (r > 0) {
                return Result.ok();
            } else {
                return Result.fail("删除失败");
            }
        }
        //按id查询电影
    @RequestMapping("/findById")
    public Result findById(@Param("id") Integer id){
        System.out.println("电影Id:"+id);
        try {

            FilmVo list=filmService.findById(id);
            return Result.ok(list);
        }catch(Exception e){
            e.printStackTrace();
        }
        return Result.fail("系统错误！");
    }

    @RequestMapping ("/save")
    public Result save(Film film){
        System.out.println(film);
        try {
            int r = filmService.save(film) ;
            if(r > 0){
                return Result.ok() ;
            }else{
                return Result.fail("保存电影失败") ;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return Result.fail("系统异常") ;
    }

    @RequestMapping("/getPageFilm")
    public Result getPageFilm(@RequestParam(defaultValue = "1") int pageNo,
                              @RequestParam(defaultValue = "8") int pageSize){
        try {
            PageInfo<FilmVo> page = filmService.getPageFilm(pageNo,pageSize) ;
            return  Result.ok(page) ;
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail("服务器内部错误") ;
    }

    @RequestMapping("/getFilmInformation")
    public Result getInformation(@RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "8") int pageSize, String filmName, String info){
        try {
            System.out.println(filmName);
            PageInfo<FilmCheckVo> orderVoPageInfo = filmService.getPageFilmInformation(pageNo, pageSize,filmName,info);
            return Result.ok(orderVoPageInfo) ;
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.fail("服务器异常") ;
    }

    @RequestMapping("/upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException {
        if(file != null){
            //System.out.println("upload 的绝对路径 ： " + path);
            String path = "E:\\lanqiao\\images";
            //判断一下 upload 文件夹是否存在
            File f = new File(path) ;
            if(!f.exists()){
                // 不存在，则创建
                f.mkdir() ;
            }
            //文件名
            String filename = file.getOriginalFilename() ;
            //获取文件后缀名
            String suffix = filename.substring(filename.lastIndexOf(".")) ;
            //重命名文件名，防止文件被覆盖
            // System.currentTimeMillis()
            String newFileName = UUID.randomUUID().toString().replaceAll("-","")+ suffix ;
            //上传的新文件
            File newFile = new File(path + "/" + newFileName);
            System.out.println("上传后的路径为 ：  " + path + "/" +newFileName);
            //把上传的file文件，保存在 upload 文件夹下（f）
            file.transferTo(newFile);
            //设置图书的图片路径
            return newFileName ; //返回图片的名称
        }

        return "" ;
    }
}
